#pragma once

#include "Misc/CommonMacros.h"

#include <atomic>
#include <memory>

#include "Containers/ThreadSafePtr.h"

#include <functional>

class ISchedule;

class WHENGINE_API CTaskBase :public EnableThreadSafeSharedPtrFromThis<CTaskBase>
{
public:
    enum class EState
    {
        JustCreated, //just created, not in schedule
        WaitingInSchedule,  //put into schedule ,not executed
        Executing,   //
        Finished,

        Failed, //when condition is not met, the task will be abadoned, not in schedule
    };

    //if return false, condition is not met, the task will be abadoned
    virtual bool Do() =0;
    virtual bool IsFinished() const;
    virtual bool IsFailed()  const;
    virtual bool IsJustCreated() const;
    void AddToSchedule();

    ISchedule*  Schedule=nullptr;
    std::atomic<CTaskBase::EState>  State;
    
    //the next task will be executed after this task
    TThreadSafeSharedPtr<CTaskBase> NextTask;

    //if the task is failed, the next task will be this task
    TThreadSafeSharedPtr<CTaskBase> FailedTask;

};